/*
	Flie:						MessageSystem.cpp
	Course:				SGP 1208
	Author:				Kori Po
	Purpose:			Message system
*/

// Using:
#include "MessageSystem.h"
using std::queue;


// SINGLETON
CMessageSystem* CMessageSystem::GetInstance(void)
{
	static CMessageSystem instance;
	return &instance;
}

//setup the function pointer
void CMessageSystem::InitMessageSystem(MESSAGEPROC m_TheProc)
{
	// Error check for valid message
	if(!m_TheProc) return;

	m_MsgProc = m_TheProc;
}

// Send the Message into the queue
void CMessageSystem::SendMsg(IMessage* pMsg)
{
	// Is there a message?
	if(!pMsg) return;

	// Send the Message
	m_MsgQueue.push(pMsg);
}

// Process all Messages
void CMessageSystem::ProcessMessages(void)
{
	// Is there a message proc?
	if(!m_MsgProc) return;

	// Processing Time
	while(!m_MsgQueue.empty())
	{
		m_MsgProc(m_MsgQueue.front());
		delete m_MsgQueue.front();
		m_MsgQueue.pop();
	}
}

// Clear any Messages
void CMessageSystem::ClearMessages(void)
{
	// Clear all Messages
	while(!m_MsgQueue.empty())
	{
		delete m_MsgQueue.front();
		m_MsgQueue.pop();
	}
}

// Close all doors
void CMessageSystem::ShutdownMessageSystem(void)
{
	ClearMessages();

	m_MsgProc = NULL;
}